package com.coderfans.htmlpaser.paser;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${fanchunshuai} on 2017-6-13.
 *
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class HtmlParser<T> {
    Logger logger = Logger.getLogger(this.getClass());

    /**
     * 将爬取到的结果放到该map里
     */
    private Map<String,Object>  resultModelMap = new HashMap<>();
    /**
     * 离线版本的html抽取工具
     * @param objXpathMap  带有xpath集合的map,与java bean和抽取模型有关
     * @param filePath     抽取的Html文件路径
     * @param clazz        抽取的结果转换成java bean
     * @return
     */
    public T  paseHtml(Map<String,String> objXpathMap,String filePath,Class<T> clazz){
        try {
            Document document = Jsoup.parse(new File(filePath), "UTF-8");

            for (Map.Entry<String, String> entry: objXpathMap.entrySet()) {
                String key = entry.getKey();
                String xpathValue = entry.getValue();
                //如果key或者属性以list,List结尾则使用
                if(key.endsWith("List") || key.endsWith("list")){
                    List<String> resultValue = Xsoup.compile(xpathValue).evaluate(document).list();
                    this.resultModelMap.put(key,resultValue);
                }else{
                    String resultValue = Xsoup.compile(xpathValue).evaluate(document).list().get(0);
                    this.resultModelMap.put(key,resultValue);
                }
            }
            //这里依赖Spring的bean包装类,
            T obj = null;
            try {
                obj = BeanUtils.instantiateClass(clazz);
                BeanWrapper beanWrapper = new BeanWrapperImpl(obj);
                beanWrapper.setPropertyValues(this.resultModelMap);
            }catch (Exception e){
                logger.error("bean 包装出错",e);
            }
            return obj;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        Document document = null;
        long start = System.currentTimeMillis();
        try {
            File file = new File("E:\\test\\aofengdaxia");

            for (File file1 : file.listFiles()){
                document = Jsoup.parse(new File(file1.getAbsolutePath()), "UTF-8");
                String  xpathValue = "//span[@class='link_categories']/a/text()";

                List<String> list = Xsoup.compile(xpathValue).evaluate(document).list();
                if(list.size() > 0){

                    System.out.println(file1.getName()+"------"+list.get(0));
                }else {

                    String xpathValue2 = "//*[@id=\"article_details\"]/div[4]/div[2]/label/span/text()";
                    List<String> list2 = Xsoup.compile(xpathValue2).evaluate(document).list();
                    if(list2.size() > 0){
                        System.out.println("filename = "+file1.getName()+", value = "+list2.get(0));
                    }else{
                        System.out.println("filename11111111111111111 = "+file1.getName());
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
